package com.xdf.xzymanagementsystem.service.impl;

import com.xdf.xzymanagementsystem.dto.SysUserDTO;
import com.xdf.xzymanagementsystem.entity.*;
import com.xdf.xzymanagementsystem.service.*;
import com.xdf.xzymanagementsystem.service.base.BaseService;
import com.xdf.xzymanagementsystem.util.CustumDozerMapperUtils;
import com.xdf.xzymanagementsystem.util.Example;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.xdf.xzymanagementsystem.dao.SysUserMapper;

/**
 * @Description : com.xdf.xzymanagementsystem.service.impl(业务层实现类,该类为生成器自动生成).
 * @Author : 张柯
 * @Date : 2020-12-31 14:42:38
 */
@Service
public class SysUserServiceImpl extends BaseService implements SysUserService {
    @Autowired
    private SysUserMapper dao;
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysMenuService menuService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private SysUserRoleGroupService userRoleGroupService;
    @Autowired
    private CustumDozerMapperUtils utils;

    /**
     * @Describe 新增方法
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(SysUser record) throws Exception {
        return dao.insert(record);
    }

    /**
     * @Describe 选择新增
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(SysUser record) throws Exception {
        return dao.insertSelective(record);
    }

    /**
     * @Describe 批量新增
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<SysUser> list) throws Exception {
        return dao.multiInsert(list);
    }

    /**
     * @Describe 修改
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(SysUser record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 选择修改
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(SysUser record) throws Exception {
        return dao.updateByPrimaryKeySelective(record);
    }

    /**
     * @Describe 修改
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(SysUser record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 批量修改
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiUpdate(List<SysUser> list) throws Exception {
        return dao.multiUpdate(list);
    }

    /**
     * @Describe 根据主键查询
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    public SysUser selectByPrimaryKey(Integer id) throws Exception {
        return dao.selectByPrimaryKey(id);
    }

    /**
     * @Describe 根据主键删除
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Integer id) throws Exception {
        return dao.deleteByPrimaryKey(id);
    }

    /**
     * @Describe 根据Entity查询
     * @author 张柯
     * @Date 2020-12-31 14:42:38
     */
    @Override
    public List<SysUser> listQuery(SysUser record) throws Exception {
        List<SysUser> ret = dao.selectByParam(record);
        ret.sort(new Comparator<SysUser>() {
            @Override
            public int compare(SysUser o1, SysUser o2) {
                int ret = o1.getLoginName().compareTo(o2.getLoginName());
                if(ret == 0) return 0;
                else return ret > 0 ? 1 : -1;
            }
        });
        return ret;
    }

    /**
     * 查询
     *
     * @param setRowSize
     * @return
     */
    @Override
    public List<SysUser> listQuery(Example setRowSize) {
        return dao.selectByExample(setRowSize);
    }

    /**
     * 根据登录用户登录账号获取
     *
     * @param loginName
     * @return
     * @throws Exception
     */
    @Override
    public SysUser selectByLoginName(String loginName) throws Exception {
        if (StringUtils.isEmpty(loginName)) return null;
        List<SysUser> list = dao.selectByParam(SysUser.builder().loginName(loginName).build());
        if (CollectionUtils.isEmpty(list)) return null;
        return list.stream().findFirst().get();
    }

    /**
     * 查询角色
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public List<SysRole> selectRolesByUserId(Integer id) throws Exception {
        Integer[] roleGroupId = Optional.ofNullable(userRoleGroupService.listQuery(SysUserRoleGroup.builder()
                .sysUserId(id)
                .build()))
                .orElse(new ArrayList<>())
                .stream().map(x -> x.getSysRoleGroupId()).distinct().toArray(Integer[]::new);
        List<SysRole> roleList = roleService.selectByRoleGroupId(roleGroupId);
        return roleList;
    }

    /**
     * 根据用户编号查询
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public List<SysPermission> selectPermissionByUserId(Integer id) throws Exception {
        List<SysRole> sysRoles = selectRolesByUserId(id);
        if (CollectionUtils.isEmpty(sysRoles)) return null;
        if (sysRoles.stream().filter(x -> x.getRoleAdminSign() != null && x.getRoleAdminSign()).count() > 0) {
            List<SysMenu> menuList = Optional.ofNullable(menuService.listQuery(SysMenu.builder().build())).orElse(new ArrayList<>());
            return menuList.stream().map(x ->
                    SysPermission.builder()
                            .sysMenuId(x.getId())
                            .sysShow(1)
                            .sysAdd(1).sysAudit(1)
                            .sysDelete(1).sysExport(1)
                            .sysModify(1).sysMoney(1)
                            .sysPrice(1).sysPrint(1)
                            .build()
            ).collect(Collectors.toList());
        }
        List<SysPermission> list = permissionService.queryRoleId(sysRoles.stream().map(x -> x.getId()).toArray(Integer[]::new));
        return list;
    }

    /**
     * 查询当前登录用户
     *
     * @return
     * @throws Exception
     */
    @Override
    public SysUserDTO selectCurrentLoginUser() throws Exception {
        Integer userId = getLoginUserId();
        SysUser sysUser = dao.selectByPrimaryKey(userId);
        return utils.transObject(sysUser, SysUserDTO.class);
    }

    /**
     * 计数
     *
     * @param example
     * @return
     */
    @Override
    public Long count(Example example) {
        return dao.countByExample(example);
    }

    @Override
    public List<SysUser> getUsers8Role(Integer roleId){
        return dao.getUsers8Role(roleId);
    }

}